package com.symaster.common.connect.listener;

import com.symaster.common.connect.local.ConnectObj;
import com.symaster.common.connect.v2.Master;
import com.symaster.common.swing.util.ThreadPoolUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.EventListener;
import java.util.HashSet;
import java.util.Set;

/**
 * @author yinmiao
 * @version 2022-05-03 19:44
 */
public class MasterListener implements EventListener {

    private Set<Master.ConnectSuccessListener> connectSuccessListeners;
    private Set<Master.ConnectClosedListener> connectClosedListeners;
    private Set<Master.MessageHandler> messageHandlers;

    public void addConnectSuccessListener(ConnectSuccessListener listener) {
        if (connectSuccessListeners == null) {
            connectSuccessListeners = new HashSet<>();
        }
        connectSuccessListeners.add(listener);
    }

    public void removeConnectSuccessListener(ConnectSuccessListener listener) {
        if (connectSuccessListeners != null) {
            connectSuccessListeners.remove(listener);
        }
    }

    public void addConnectClosedListener(ConnectClosedListener listener) {
        if (connectClosedListeners == null) {
            connectClosedListeners = new HashSet<>();
        }
        connectClosedListeners.add(listener);
    }

    public void removeConnectClosedListener(ConnectClosedListener listener) {
        if (connectClosedListeners != null) {
            connectClosedListeners.remove(listener);
        }
    }

    public void addMessageHandler(MessageHandler handler) {
        if (messageHandlers == null) {
            messageHandlers = new HashSet<>();
        }
        messageHandlers.add(handler);
    }

    public void removeMessageHandler(MessageHandler handler) {
        if (messageHandlers != null) {
            messageHandlers.remove(handler);
        }
    }

    public void connectSuccessListener(ConnectObj connectObj) {
        if (connectSuccessListeners != null) {
            ThreadPoolUtil.execute(() -> {
                for (ConnectSuccessListener connectSuccessListener : connectSuccessListeners) {
                    connectSuccessListener.handler(connectObj);
                }
            });
        }
    }

    public void connectClosedListener(ConnectObj connectObj, int status, Throwable e) {
        if (connectClosedListeners != null) {
            ThreadPoolUtil.execute(() -> {
                for (ConnectClosedListener connectClosedListener : connectClosedListeners) {
                    connectClosedListener.handler(connectObj, status, e);
                }
            });
        }
    }

    public String messageHandler(ConnectObj connectObj, String message) {
        if (messageHandlers != null) {
            for (MessageHandler messageHandler : messageHandlers) {
                String handler = messageHandler.handler(connectObj, message);
                if (StringUtils.isNotEmpty(handler)) {
                    return handler;
                }
            }
        }
        return null;
    }

    public interface ConnectSuccessListener {
        void handler(ConnectObj connectObj);
    }

    public interface ConnectClosedListener {
        void handler(ConnectObj connectObj, int status, Throwable e);
    }

    public interface MessageHandler {
        String handler(ConnectObj connectObj, String message);
    }
}
